<template>
  <div class="login-container">
    <div class="login-bg"></div>
    <div class="login-content">
      <div class="login-card">
        <div class="logo">
          <h1>运维系统</h1>
          <p class="subtitle">Operations Management System</p>
        </div>

        <van-cell-group class="input-group" :border="false">
          <div class="input-wrapper">
            <input class="input-item" type="text" v-model="formData.mobilePhone" placeholder="账号" />
          </div>

          <div class="input-wrapper password-wrapper">
            <input class="input-item" :type="passwordVisual ? 'text' : 'password'" placeholder="密码"
              v-model="formData.password" />
            <van-icon :name="passwordVisual ? passwordInvisibleIcon : passwordVisualIcon" class="password-icon"
              @click="passwordVisual = !passwordVisual" size="20" />
          </div>
        </van-cell-group>

        <div class="actions">
          <span class="modify-link" @click="openModifyPassword">修改密码</span>
        </div>

        <div class="login-btn-container">
          <van-button class="login-btn" type="primary" block @click="onLogin">
            登录
          </van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { encrypt, setToken } from '@/utils';
import { SystemController } from '@/api';
import accountIcon from '@/assets/login/account-icon2x.png';
import passwordIcon from '@/assets/login/password-icon2x.png';
import passwordVisualIcon from '@/assets/login/password-visual-icon2x.png';
import passwordInvisibleIcon from '@/assets/login/password-invisible-icon2x.png';

export default {
  name: 'login',
  components: {},
  data() {
    return {
      accountIcon: accountIcon,
      passwordIcon: passwordIcon,
      passwordVisualIcon: passwordVisualIcon,
      passwordInvisibleIcon: passwordInvisibleIcon,
      passwordVisual: false,
      RememberPassword: false,
      formData: {
        mobilePhone: undefined,
        password: undefined
      }
    }
  },
  created() {
    this.formData.mobilePhone = localStorage.getItem('userName') || ''
    this.formData.password = localStorage.getItem('password') || ''
  },
  methods: {
    onLogin() {
      this.login();
    },
    login(verifyParams) {
      let params = {
        loginName: this.formData.mobilePhone,
        password: encrypt(this.formData.password),
        captchaVerification: (verifyParams || {}).captchaVerification
      };
      setToken(null);
      SystemController.loginMobile(this, params, null, { showMask: false }).then(data => {
        for (let item of data.data.mobileEntryList || []) {
          if (item.extraData != null && item.extraData !== '') {
            try {
              item.extraData = JSON.parse(item.extraData);
            } catch (e) {
              item.extraData = null;
            }
          } else {
            item.extraData = null;
          }
        }
        if (this.RememberPassword) {
          localStorage.setItem('userName', this.formData.mobilePhone)
          localStorage.setItem('password', this.formData.password)
        }
        this.$store.commit('setHomeEntryList', data.data.mobileEntryList)
        setToken(data.data.tokenData);
        this.$router.replace({ name: 'home' });
      }).catch(e => {
        console.log(e);
      });
    },
    onVerifySuccess(verifyParams) {
      this.login(verifyParams);
    },
    openModifyPassword() {
      this.$router.push({ name: 'password' });
    }
  }
};
</script>

<style scoped>
/* 重新设计为大气简约风格 */

.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* background-image: url('../../assets/login/login2.png'); */
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  opacity: 0.15;
  z-index: 0;
}

.login-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 440px;
  padding: 0 24px;
}

.login-card {
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  padding: 56px 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.2);
}

.logo {
  text-align: center;
  margin-bottom: 48px;
}

.logo h1 {
  font-size: 32px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 8px 0;
  letter-spacing: 2px;
}

.logo .subtitle {
  font-size: 13px;
  color: #999;
  margin: 0;
  font-weight: 400;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.input-group {
  margin-bottom: 34px;
}

.input-wrapper {
  position: relative;
  margin-bottom: 40px;
}

.input-wrapper:last-child {
  margin-bottom: 0;
}

.input-item {
  width: 100%;
  height: 58px !important;
  border: 2px solid #e8e8e8;
  border-radius: 12px;
  padding: 0 20px;
  box-sizing: border-box;
  font-size: 15px;
  color: #333;
  background-color: #fafafa;
  transition: all 0.3s ease;
}

.input-item::placeholder {
  color: #aaa;
  font-weight: 400;
}

.input-item:focus {
  outline: none;
  border-color: #667eea;
  background-color: #fff;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

.password-wrapper {
  position: relative;
}

.password-icon {
  position: absolute;
  top: 50%;
  right: 18px;
  transform: translateY(-50%);
  color: #aaa;
  cursor: pointer;
  transition: color 0.3s ease;
}

.password-icon:hover {
  color: #667eea;
}

.actions {
  text-align: right;
  margin-bottom: 32px;
}

.modify-link {
  color: #667eea;
  font-size: 14px;
  cursor: pointer;
  font-weight: 500;
  transition: color 0.3s ease;
}

.modify-link:hover {
  color: #764ba2;
  text-decoration: underline;
}

.login-btn-container {
  margin: 0;
}

.login-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  border: none !important;
  border-radius: 12px !important;
  color: #ffffff !important;
  font-size: 16px !important;
  font-weight: 600 !important;
  height: 52px !important;
  line-height: 52px !important;
  letter-spacing: 1px !important;
  transition: all 0.3s ease !important;
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3) !important;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 28px rgba(102, 126, 234, 0.4) !important;
}

.login-btn:active {
  transform: translateY(0);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    padding: 40px 28px;
    border-radius: 20px;
  }

  .logo h1 {
    font-size: 28px;
  }

  .input-item {
    height: 48px;
    font-size: 14px;
  }

  .login-btn {
    height: 48px !important;
    line-height: 48px !important;
    font-size: 15px !important;
  }
}
</style>

<style>
/* vant 样式修改 */
.van-field__left-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
}

input:focus {
  outline: none;
}
</style>
